package api.controller;

import api.model.sifas.SIFASCardInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Random;

/**
 * @author xiakai
 * @create 2021-04-03 15:16
 */
@Controller
@RequestMapping("/sifas")
public class SIFASController {

    static Random random;

    static ArrayList<SIFASCardInfo> cardInfoList;
    static ArrayList<SIFASCardInfo> transCartInfoList;

    @PostConstruct
    public void init() throws Exception {

        random = new Random();
        cardInfoList = dealCardInfo("sifas-card.txt");
        transCartInfoList = dealCardInfo("sifas-trans-card.txt");
    }



    @RequestMapping("/card")
    @ResponseBody
    public ModelAndView randomCard(@RequestParam(name = "type", defaultValue = "img") String type) throws IOException {
        if (type.equals("img")) {
            return new ModelAndView("redirect:" + getRandomCardInfo().getUrl());
        }
        if (type.equals("json")) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("data", getRandomCardInfo());
            return mav;
        }
        return null;
    }

    @RequestMapping("/trans-card")
    @ResponseBody
    public ModelAndView randomTransCard(@RequestParam(name = "type", defaultValue = "img") String type) throws IOException {
        if (type.equals("img")) {
            return new ModelAndView("redirect:" + getRandomTransCardInfo().getUrl());
        }
        if (type.equals("json")) {
            ModelAndView mav = new ModelAndView(new MappingJackson2JsonView());
            mav.addObject("data", getRandomTransCardInfo());
            return mav;
        }
        return null;
    }

    private SIFASCardInfo getRandomCardInfo() {
        return cardInfoList.get(random.nextInt(cardInfoList.size()));
    }

    private SIFASCardInfo getRandomTransCardInfo() {
        return transCartInfoList.get(random.nextInt(transCartInfoList.size()));
    }

    private ArrayList<SIFASCardInfo> dealCardInfo(String txtName) throws IOException {

        // 大图
        ArrayList<SIFASCardInfo> cardInfoList = new ArrayList<>();
        File file = new File(txtName);
        BufferedReader br = new BufferedReader(new FileReader(file));
        String address = br.readLine();
        String line = null;
        while ((line = br.readLine()) != null) {
            SIFASCardInfo cardInfo = new SIFASCardInfo();
            String[] infoGroup = line.split("-");
            cardInfo.setName(line);
            cardInfo.setId(infoGroup[1]);
            cardInfo.setTeam(infoGroup[2]);
            cardInfo.setIdolName(infoGroup[3]);
            cardInfo.setStatus(infoGroup[4]);
            cardInfo.setRarity(infoGroup[5]);
            cardInfo.setSkill(infoGroup[6].substring(0, infoGroup[6].lastIndexOf('.')));
            cardInfo.setUrl(address + URLEncoder.encode(line, "UTF-8").replaceAll("\\+", "%20"));
            cardInfoList.add(cardInfo);
        }
        br.close();
        return cardInfoList;
    }





}